<template>
	<view>
		<view class="w-box">
			<view class="w-box-title">
				<text>汇报人员</text>
			</view>
			<view class="w-box-area w-box-area-add">
				<view>{{Reporter_Name}}</view>
				<navigator :url="url">
					<view class="w-box-area-add-btn">+</view>
				</navigator>
			</view>
		</view>
		<view class="w-box">
			<view class="w-box-title">
				<text>本月工作总结</text>
			</view>
			<view class="w-box-area">
				<editor placeholder="请输入" id="s_editor" @input="onSummary" @ready="onSummaryReady"></editor>
			</view>
		</view>
		<view class="w-box">
			<view class="w-box-title">
				<text>下月工作计划</text>
			</view>
			<view class="w-box-area">
				<editor placeholder="请输入" id="p_editor" @input="onPlan" @ready="onPlanReady"></editor>
			</view>
		</view>
		<view class="w-box">
			<view class="w-box-title">
				<text>选择年份</text>
			</view>
			<view class="w-box-area">
				<picker mode="date" fields="year" @change="onBindWriteYear">
					<view>{{ReportYear}}</view>
				</picker>
			</view>
		</view>
		<view class="w-box">
			<view class="w-box-title">
				<text>选择月份</text>
			</view>
			<view class="w-box-area">
				<picker mode="date" fields="month" @change="onBindWriteWeekly">
					<view>{{ReportMonth}}</view>
				</picker>
			</view>
		</view>
		<view class="w-btns">
			<view>
				<button type="primary" :disabled="blnDisabled_w" class="c-input-btn" @click="onWriteRep">提交月报</button>
			</view>
		</view>
	</view>
</template>

<script>
	let pageApp = getApp();
	export default {
		data() {
			return {
				info: {},
				Summary: "",
				Plan: "",
				id: 0,
				Reporter_Name: "",
				ReportYear: "请选择",
				ReportMonth: "请选择",
				blnDisabled_w: true,
				blnEditStatus: false,
				url: "./MonthlyUserList"
			}
		},
		onLoad: function(options) {
			let id = options.id; //编辑状态
			let type = options.type; //修改汇报人员
			this.id = id;
			this.type = type
			this.Reporter_Name = type ? uni.getStorageSync("searchUser").UserName : JSON.parse(uni.getStorageSync(
				"userLoginData")).UserName;
			if (id) {
				this.url = this.url + '?id=' + id;
				this.blnEditStatus = true;
				// 准备请求数据
				let apiData = {
					url: pageApp.globalData.url + "/MonthlyReportInfo",
					data: {
						"ID": this.id
					},
					success: (d) => {
						let _Reporter_Name = type ? uni.getStorageSync("searchUser").UserName : d.data.data.Reporter_Name;
						this.info.Summary = this.Base64.unescape(d.data.data.Summary);
						this.info.Plan = this.Base64.unescape(d.data.data.Plan);
						this.ReportYear = d.data.data.ReportYear;
						this.ReportMonth = d.data.data.ReportMonth;
						this.Reporter_Name = _Reporter_Name;
						this.blnDisabled_w = false;
					}
				}
				// 发送数据请求
				this.vjAjaxData(apiData);
			}
		},
		methods: {
			onBindWriteYear(e) {
				this.ReportYear = new Date(e.target.value).getFullYear();
			},
			onBindWriteWeekly(e) {
				this.ReportMonth = new Date(e.target.value).getMonth() + 1;
			},
			onSummary(e) {
				this.Summary = e.detail.text;
				this.blnDisabled_w = this.checkIsEmpty(this.Summary, this.Plan);
			},
			onPlan(e) {
				this.Plan = e.detail.text
				this.blnDisabled_w = this.checkIsEmpty(this.Summary, this.Plan);
			},
			onSummaryReady(e) {
				if (this.blnEditStatus) {
					uni.createSelectorQuery().select('#s_editor').context((res) => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.info.Summary
						})
					}).exec()
				}
			},
			onPlanReady(e) {
				if (this.blnEditStatus) {
					uni.createSelectorQuery().select('#p_editor').context((res) => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.info.Plan
						})
					}).exec()
				}
			},
			onWriteRep() {
				if (this.blnEditStatus) {
					// 准备请求数据
					let apiData = {
						url: pageApp.globalData.url + "/MonthlyReportUpdate",
						data: {
							"ID": this.id,
							"Summary": this.Summary == "" ? this.info.Summary : this.Summary,
							"Plan": this.Plan == "" ? this.info.Plan : this.Plan,
							"Reporter": this.type ? uni.getStorageSync("searchUser").ID : JSON.parse(uni.getStorageSync(
								"userLoginData")).UserID,
							"ReportYear": this.ReportYear,
							"ReportMonth": this.ReportMonth,
							"DepartmentID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
							"OrgID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0
						},
						method: "POST",
						success: (d) => {
							uni.hideLoading();
							if (d.data.code == "001") {
								uni.showToast({
									title: "修改成功"
								})
								uni.navigateTo({
									url: "./MonthlyRepList"
								});
							} else {
								uni.showToast({
									icon: "none",
									title: d.data.msg
								})
							}
						}
					}
					// 发送数据请求
					this.vjAjaxData(apiData);
				} else {
					// 准备请求数据
					let apiData = {
						url: pageApp.globalData.url + "/MonthlyReportNew",
						data: {
							"Summary": this.Summary,
							"Plan": this.Plan,
							"Reporter": this.type ? uni.getStorageSync("searchUser").ID : JSON.parse(uni.getStorageSync(
								"userLoginData")).UserID,
							"ReportYear": this.ReportYear,
							"ReportMonth": this.ReportMonth,
							"DepartmentID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0,
							"OrgID": uni.getStorageSync("userLoginData") ? JSON.parse(uni.getStorageSync("userLoginData")).OrgID : 0
						},
						method: "POST",
						success: (d) => {
							uni.hideLoading();
							if (d.data.code == "001") {
								uni.showToast({
									title: "增加成功"
								})
								uni.navigateTo({
									url: "./MonthlyRepList"
								});
							} else {
								uni.showToast({
									icon: "none",
									title: d.data.msg
								})
							}

						}
					}
					// 发送数据请求
					this.vjAjaxData(apiData);
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #F1F5F6;
	}

	.ql-container {
		height: 100rpx;
		min-height: 100rpx;
	}

	.w-box {
		background-color: #FFFFFF;
		margin-bottom: 2rpx;
		padding: 40rpx;
	}

	.w-box-title {
		font-size: 32rpx;
		color: #000000;
		font-weight: 700;
	}

	.w-box-area-add {
		display: flex;
		justify-content: space-between;
	}

	.w-box-area-add-btn {
		width: 55rpx;
		text-align: center;
		font-size: 50rpx;
		height: 55rpx;
		background-color: #4497e1;
		color: #fff;
		border-radius: 50%;
	}

	.w-btns {
		display: flex;
		justify-content: flex-end;
		position: fixed;
		right: 0;
		bottom: 0;
		width: 100%;
		background-color: #FFFFFF;
		padding: 10rpx;
	}

	.c-input-btn {
		width: 500rpx;
	}
</style>
